package com.tdzh.maimaiti.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tdzh.maimaiti.R;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MyPageFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MyPageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyPageFragment extends BaseFragment {

    private TextView userText;
    private ImageButton friendButton;
    private ImageButton interestButton;
    private OnFragmentInteractionListener mListener;

    public MyPageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

       View view =  inflater.inflate(R.layout.fragment_my_page, container, false);
        interestButton = (ImageButton) view.findViewById(R.id.myInterestButton);
        friendButton = (ImageButton) view.findViewById(R.id.myFriendButton);
        userText = (TextView) view.findViewById(R.id.username);
        SharedPreferences user = getActivity().getSharedPreferences("account",0);
        userText.setText(user.getString("name", null));
        System.out.println("MyPage user: " + user.getString("name", null));

        interestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("fNum","M");
                bundle.putString("bNum","1");
                mListener.onFragmentInteraction(bundle, v);
            }
        });


        friendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(getActivity(),ContractListActivity.class);
                //startActivity(intent);
                Bundle bundle = new Bundle();
                bundle.putString("fNum","M");
                bundle.putString("bNum","4");
                mListener.onFragmentInteraction(bundle, v);

            }
        });
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(int pos) {
        if (mListener != null) {
            System.out.println("MyPageFragment");
          //  mListener.onFragmentInteraction(pos);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
}
